package com.cloud.system.controller;

import com.github.pagehelper.PageHelper;
import com.cloud.common.constants.ExcelPoiConstants;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.entity.SysOperLogs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.system.service.ISysOperLogsService;
import com.cloud.utils.ExcelPoiUtils;
import com.cloud.utils.ServletUtils;
import com.cloud.utils.ToolUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志记录
 *
 * @author zx, yzj
 */
@Tag(name = "日志管理")
@RestController
@RequestMapping("/system/operLogs")
public class SysOperLogsController extends BaseController {

    @Autowired
    private ISysOperLogsService logOperService;

    @Operation(summary = "查询列表")
    @RequestPermissions("system:operLogs:list")
    @GetMapping("/list")
    public Result list(SysOperLogs operLogs) {
        startPage();
        List<SysOperLogs> list = logOperService.selectLogOperList(operLogs);
        return getDataTable(list);
    }

    @Operation(summary = "导出日志")
    @RequestPermissions("system:operLogs:export")
    @GetMapping("/export")
    public void export(SysOperLogs operLogs) {
        PageHelper.startPage(ExcelPoiConstants.startNum, ExcelPoiConstants.maxSize);
        List<SysOperLogs> list = logOperService.selectLogOperList(operLogs);
        String titleName = "操作日志列表";
        ExcelPoiUtils.exportExcel(list, titleName, "sys_log_login", SysOperLogs.class,
                titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "删除日志")
    @Parameter(name = "operIds", description = "日志ID[多个用,分割]")
    @RequestPermissions("system:operLogs:remove")
    @PostMapping("/remove")
    public Result remove(String operIds) {
        List<Long> list = ToolUtils.setIdsToList(operIds);
        return toResult(logOperService.deleteLogOperByIds(list));
    }

    @Operation(summary = "清空日志")
    @RequestPermissions("system:operLogs:remove")
    @GetMapping("/clean")
    public Result clean() {
        logOperService.cleanLogOper();
        return Result.success("操作成功");
    }

    // TODO Feign内部调用（添加操作日志）
    @Operation(summary = "添加操作日志", hidden = true)
    @PostMapping("/inAuth/saveOperLogs")
    public Result saveOperLogs(@RequestBody SysOperLogs operLogs) {
        return toResult(logOperService.insertLogOper(operLogs));
    }

}
